package ge.lowlevel.js.box2d;

 
import ge.lowlevel.box2d.BoxGe;
import ge.lowlevel.box2d.WorldGe;
import ge.modules.physics.PhysicsAttributes;

import com.google.gwt.core.client.JavaScriptObject;

public class BoxJs extends BoxGe<JavaScriptObject>
{
	private JavaScriptObject box;
	private JavaScriptObject defbox;
	public BoxJs(WorldJs world,PhysicsAttributes pAtt,double width, double height,double x, double y)
	{
		//Super call does nothing
		super(world, pAtt, width, height, x, y);
		init(world,pAtt.density, pAtt.restitution,pAtt.friction, pAtt.fixedRotation,width, height,x,y);
	}
	
	
	private native void init(WorldJs world,double density, double restitution,double friction, boolean fixedRot, double width, double height,double x, double y)
	/*-{
		var boxSd = new $wnd.b2BoxDef();
		boxSd.density = density; 
		boxSd.restitution = restitution;
		boxSd.friction = friction;
		boxSd.extents.Set(width, height);
	
		//boxSd.type = $wnd.b2Body.2;
		var boxBd = new $wnd.b2BodyDef();
		boxBd.preventRotation = fixedRot;
			//$wnd.alert(boxBd.m_type);
		boxBd.AddShape(boxSd);
		boxBd.position.Set(x,y);
		this.@ge.lowlevel.js.box2d.BoxJs::defbox = boxBd;
		var w = world.@ge.lowlevel.js.box2d.WorldJs::getWorld()();
		
		this.@ge.lowlevel.js.box2d.BoxJs::box = w.CreateBody(boxBd);
		//$wnd.alert(this.@ge.lowlevel.js.box2d.BoxJs::box);
		
	}-*/;
	
	@Override
	public native void setPos(double x, double y)	
	/*-{
	     this.@ge.lowlevel.js.box2d.BoxJs::box.WakeUp();
		this.@ge.lowlevel.js.box2d.BoxJs::box.m_position.Set(x,y);	
		 
	}-*/;
	@Override
	public final native double getX()
	/*-{
		return this.@ge.lowlevel.js.box2d.BoxJs::box.m_position.x;	
	}-*/;
	@Override
	public final native double getY()
	/*-{
		return this.@ge.lowlevel.js.box2d.BoxJs::box.m_position.y;	
	}-*/;
	@Override
	public final native double getXLinealVelocity()
	/*-{
		return this.@ge.lowlevel.js.box2d.BoxJs::box.m_linearVelocity.x;	
	}-*/;
	public final native double getYLinealVelocity()
	/*-{
		return this.@ge.lowlevel.js.box2d.BoxJs::box.m_linearVelocity.y;	
	}-*/;
	
	public final native void setLinealVelocity(double x, double y)
	/*-{
		 this.@ge.lowlevel.js.box2d.BoxJs::box.m_linearVelocity.Set(x,y);	
	}-*/;
 
	public final native void setFixedRotation(boolean fixed)
	/*-{
		 this.@ge.lowlevel.js.box2d.BoxJs::defbox.preventRotation = fixed;
		 $wnd.alert( this.@ge.lowlevel.js.box2d.BoxJs::defbox.preventRotation);
	}-*/;
	@Override
	public  native void applyImpulse(double fx, double fy, double x, double y)
	/*-{
	 	var body = this.@ge.lowlevel.js.box2d.BoxJs::box;
	 	body.WakeUp();
	  	body.ApplyImpulse(new $wnd.b2Vec2(fx,fy),new $wnd.b2Vec2(x,y));
	}-*/;
	@Override
	public final native String getPoints(int numShape)
	/*-{
 		var body = this.@ge.lowlevel.js.box2d.BoxJs::box;
  		var points ="";
  	 	var shape;
  	 	var index = 0;
  	 	var s;
  	    for (s = body.GetShapeList(); s != null; s = s.GetNext()) 
  	    {
  	    	if(index == numShape)
  	    		break;
  	    }	
  	    
  	    if(index < body.m_shapeCount)
  	    {
  	    	shape = s;
			for (var i = 0; i < shape.m_vertexCount; i++) 
			{
					//var v = $wnd.b2Math.AddVV(shape.m_position, $wnd.b2Math.b2MulMV(shape.m_R, shape.m_vertices[i]));
					var v =  $wnd.b2Math.b2MulMV(shape.m_R, shape.m_vertices[i]);
					points += v.x + " " +v.y + " ";
			}
  	    }
		return points;
	}-*/;
	
	@Override
	public native final void destroyBody(WorldGe world)
	/*-{
	 	var body = this.@ge.lowlevel.js.box2d.BoxJs::box;
		var w = world.@ge.lowlevel.js.box2d.WorldJs::getWorld()();
		w.DestroyBody(body);
	}-*/;
	@Override
	public JavaScriptObject getBox()  {
		return box;
	}
	
	 
}
